﻿using System;
using System.Web.UI.WebControls;
using DAL.Repositories;
using GameAdmin.Classes;

namespace GameAdmin.Pages
{
    public partial class scores : System.Web.UI.Page
    {
        #region Page variables (paging)

        private int? _PageIndex;
        private int PageIndex
        {
            get
            {
                if (_PageIndex == null)
                {
                    if (!String.IsNullOrEmpty((string)RouteData.Values["page"])) _PageIndex = Int32.Parse((string)RouteData.Values["page"]);
                    else _PageIndex = 1;
                }
                return (int)_PageIndex;
            }
        }

        private int PagingLimit = 20;

        private int PagingSkip
        {
            get
            {
                return (PageIndex - 1) * PagingLimit + 1;
            }
        }

        private int TotalItems;

        #endregion

        protected void Page_Init(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude("javascript_jquery", "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
            Page.ClientScript.RegisterClientScriptInclude("javascript_jquery-ui", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js");
            Page.ClientScript.RegisterClientScriptInclude("javascript_cs", "/Js/jquery.ui.datepicker-cs.js");

            btnFilter.Click += new EventHandler(btnFilter_Click);
            gw.RowDeleting += new GridViewDeleteEventHandler(gw_RowDeleting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetFilter();
                GetScores();
                GetPagination();
            }
        }

        void btnFilter_Click(object sender, EventArgs e)
        {
            if (txtSince.Text == "") MySession.Current.DateSince = new DateTime();
            else MySession.Current.DateSince = DateTime.Parse(txtSince.Text);

            if (txtTill.Text == "") MySession.Current.DateTill = new DateTime();
            else MySession.Current.DateTill = DateTime.Parse(txtTill.Text);

            GetScores();
            GetPagination();
        }

        void gw_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int idToDelete = Int32.Parse(gw.Rows[e.RowIndex].Cells[0].Text);
            ScoreRepository.Delete(idToDelete);

            GetScores();
            GetPagination();
        }

        private void GetFilter()
        {
            if (MySession.Current.DateSince != new DateTime(2000, 1, 1)) txtSince.Text = MySession.Current.DateSince.ToShortDateString();
            if (MySession.Current.DateTill != new DateTime(2050, 1, 1)) txtTill.Text = MySession.Current.DateTill.ToShortDateString();
        }

        private void GetScores()
        {
            gw.DataSource = ScoreRepository.GetScores(PagingSkip, PagingLimit, out TotalItems, MySession.Current.DateSince, MySession.Current.DateTill);
            gw.DataBind();
        }

        private void GetPagination()
        {
            HelperMethods.AddPagination(TotalItems, PagingLimit, PageIndex, "~/scores/", pnlPagination);
        }
    }
}